﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.IO
Imports SpeechLib
Imports Microsoft.Speech.Synthesis

Public Class frmMain
    Public Timer()
    Dim VirtuarlKeyboard1 As PictureBox
    Dim VirtuarlKeyboard2 As PictureBox
    Friend WithEvents txtPaliLatin As PalilatinKeyboard
    Friend WithEvents txtUSKeyBoard As EnglishUSKeyboard
    Friend WithEvents txtPinYin As PinyinKeyboard
    Public WithEvents vox As New SpVoice

    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer
    Dim FormMax As Boolean
    Dim FaceFont As String
    Public ds As New DataSet()
    Private cn As New OleDbConnection()
    Dim cmd As New OleDbCommand()

    Dim strWord1 As String
    Dim strContent As String
    Public DictCode As String
    Public DictName As String
    Public DictAuthor As String
    Public isWordSelect As Boolean
    Public DictFont As String
    Public DictTable As String
    Dim bCancelLink As Boolean
    Dim myHTML As DicHTML()
    Dim myIO As IOTummo()
    Dim LockCombo As Integer

    Private Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer

    Private Structure APPBARDATA
        Dim cbSize As Integer
        Dim hwnd As IntPtr
        Dim uCallbackMessage As Integer
        Dim uEdge As Integer
        Dim rc As RECT
        Dim lParam As Integer
    End Structure

    Private Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Const ABM_GETTASKBARPOS = &H5
    Const ABM_GETSTATE = &H4

    Private Sub GetMax()
        Dim ABD As APPBARDATA
        Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim screenHeight As Integer = (Screen.PrimaryScreen.Bounds.Height) - SystemInformation.CaptionHeight

        SHAppBarMessage(ABM_GETTASKBARPOS, ABD)
        'MsgBox("Width:" & ABD.rc.Right - ABD.rc.Left)
        'MsgBox(" Height:" & ABD.rc.Bottom - ABD.rc.Top)
        Me.Location = New Point(0, 0)
        Me.Height = screenHeight - (screenHeight - ABD.rc.Top)
        Me.Width = screenWidth

        ReTableSize()
    End Sub

    Private Sub ValueKeyboard()
        If cboKeyboard.Items.Count = 0 Then
            cboKeyboard.Items.Add("English - US")
            cboKeyboard.Items.Add("Pali - Latin")
            cboKeyboard.Items.Add("Chinese - PinYin")
            cboKeyboard.Text = "English - US"
        End If
        
    End Sub

    Private Sub DrawTextbox()
        Dim x As Integer
        ' Remove
        Me.Controls.Remove(Me.txtUSKeyBoard)
        Me.Controls.Remove(Me.txtPaliLatin)
        Me.Controls.Remove(Me.txtPinYin)

        Me.Controls.Remove(Me.VirtuarlKeyboard1)
        Me.Controls.Remove(Me.VirtuarlKeyboard2)

        x = 58
        If cboKeyboard.Text = "English - US" Then
            txtUSKeyBoard = New EnglishUSKeyboard

            txtUSKeyBoard.Width = 329
            txtUSKeyBoard.Height = 22
            txtUSKeyBoard.Location = New System.Drawing.Point(30, 93 - x)

            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtUSKeyBoard})
            txtUSKeyBoard.BringToFront()

            Me.Controls.Remove(Me.txtPaliLatin)
            Me.Controls.Remove(Me.VirtuarlKeyboard1)
        ElseIf cboKeyboard.Text = "Pali - Latin" Then
            Dim bmp As Bitmap = Global.Tummo_Dictionary.My.Resources.Pali_Keyboard_1
            bmp.MakeTransparent(Color.White) ' magenta in bitmap will be transparent

            txtPaliLatin = New PalilatinKeyboard
            VirtuarlKeyboard1 = New PictureBox

            txtPaliLatin.Width = 329
            txtPaliLatin.Height = 22
            txtPaliLatin.Location = New System.Drawing.Point(30, 93 - x)

            VirtuarlKeyboard1.Width = 280
            VirtuarlKeyboard1.Height = 70
            VirtuarlKeyboard1.Location = New System.Drawing.Point(30, 116 - x)
            VirtuarlKeyboard1.Image = bmp
            VirtuarlKeyboard1.BackColor = Color.Transparent

            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtPaliLatin})
            txtPaliLatin.BringToFront()
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.VirtuarlKeyboard1})
            VirtuarlKeyboard1.BringToFront()
        ElseIf cboKeyboard.Text = "Chinese - PinYin" Then
            Dim bmp As Bitmap = Global.Tummo_Dictionary.My.Resources.PinYinKeyboard
            bmp.MakeTransparent(Color.White) ' magenta in bitmap will be transparent

            txtPinYin = New PinyinKeyboard
            VirtuarlKeyboard2 = New PictureBox

            txtPinYin.Width = 329
            txtPinYin.Height = 22
            txtPinYin.Location = New System.Drawing.Point(30, 93 - x)

            VirtuarlKeyboard2.Width = 335
            VirtuarlKeyboard2.Height = 90
            VirtuarlKeyboard2.Location = New System.Drawing.Point(30, 116 - x)
            VirtuarlKeyboard2.Image = bmp
            VirtuarlKeyboard2.BackColor = Color.Transparent

            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtPinYin})
            txtPinYin.BringToFront()
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.VirtuarlKeyboard2})
            VirtuarlKeyboard2.BringToFront()
        End If

    End Sub

    Private Sub ReTableSize()
        Dim columnsWidth As Double

        If listWords.ColumnCount > 0 Then
            columnsWidth = (SplitContainer1.Panel1.Width / 2) - 2
            listWords.Columns(0).Width = columnsWidth
            listWords.Columns(1).Width = columnsWidth
            listWords.Columns(0).ReadOnly = True
            listWords.Columns(1).ReadOnly = True
        End If
    End Sub

    Private Sub GetNormal()
        Me.CenterToScreen()

        ReTableSize()
    End Sub

    Private Sub TwoColor()
        Dim i As Integer

        For i = 0 To listWords.Rows.Count - 1
            If (i Mod 2) = 1 Then
                listWords.Item(0, i).Style.BackColor = Color.LightBlue
                listWords.Item(1, i).Style.BackColor = Color.LightBlue
            Else
                listWords.Item(0, i).Style.BackColor = Color.Azure
                listWords.Item(1, i).Style.BackColor = Color.Azure
            End If
        Next
    End Sub

    Private Sub LoadLanguage()
        Dim oFile As System.IO.File ' Read text file
        Dim oDir As System.IO.Directory
        Dim oRead As System.IO.StreamReader ' --		
        Dim lastWords, strKey As String
        Dim lenWords, lenKey, fWidth As Integer
        Dim myIO As New IOTummo

        If myIO.LanguageFileExits = True Then
            oRead = oFile.OpenText(myIO.LanguagePath)
            Do While Not oRead.EndOfStream
                lastWords = oRead.ReadLine()
                lenWords = Len(lastWords)
                strKey = "[language-font]"
                lenKey = Len(strKey)
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    FaceFont = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                End If
                strKey = "[main-search1]"
                lenKey = Len(strKey)
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    lbSearchBy.Text = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                End If
                strKey = "[main-search2]"
                lenKey = Len(strKey)
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    checkWord1.Text = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                    checkSpeaker1.Text = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                End If
                strKey = "[main-search3]"
                lenKey = Len(strKey)
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    checkWord2.Text = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                End If
                strKey = "[main-search4]"
                lenKey = Len(strKey)
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    checkSpeaker2.Text = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                End If
                strKey = "[main-column1]"
                lenKey = Len(strKey)
                'MsgBox("have")
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    'listWords.Columns(0).HeaderText = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                    'Me.Location = New Point(fWidth, fHeight)
                End If
                strKey = "[main-column2]"
                lenKey = Len(strKey)
                'MsgBox("have")
                If LCase(Mid(lastWords, 1, lenKey)) = strKey Then
                    'listWords.Columns(1).HeaderText = Mid(lastWords, lenKey + 3, lenWords - (lenKey + 3))
                    'Me.Location = New Point(fWidth, fHeight)
                End If
            Loop
            oRead.Close() ' Close text file

            ' Dat lai vi tri

            fWidth = checkWord1.Location.X + checkWord1.Width + 5
            checkWord2.Location = New Point(fWidth, 28)

            fWidth = checkSpeaker1.Location.X + checkSpeaker1.Width + 7
            checkSpeaker2.Location = New Point(fWidth, 6)

            fWidth = checkSpeaker2.Location.X + checkSpeaker2.Width + 5
            btSpeaker.Location = New Point(fWidth, 2)

            fWidth = btSpeaker.Location.X + btSpeaker.Width + 2
            btStopSpeaker.Location = New Point(fWidth, 2)
            ' Cai dat font chu
            lbSearchBy.Font = New System.Drawing.Font(FaceFont, 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            checkWord1.Font = New System.Drawing.Font(FaceFont, 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            checkWord2.Font = New System.Drawing.Font(FaceFont, 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

        Else
            MsgBox("Err 01: Missing language file!", MsgBoxStyle.Exclamation, "Alarm")
        End If
    End Sub


    Private Sub LoadCache2(ByVal TableName As String)
        Dim sCurrentDir As String

        If TableName = "" Then Exit Sub

        frmPleaseWait.Show()
        Me.AddOwnedForm(frmPleaseWait)

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 

        cn.ConnectionString = stConnectDB
        cn.Open()
        Dim da As New OleDbDataAdapter("SELECT Words,Transcription FROM " & TableName, cn)

        da.FillSchema(ds, SchemaType.Source, TableName)
        da.Fill(ds, TableName)
        Dim cmdBuilder As New OleDbCommandBuilder(da)
        Dim dv As New DataView()

        listWords.DataSource = ds.Tables(TableName)
        'TwoColor()
        cn.Close()

        frmPleaseWait.Close()
    End Sub

    Public Sub SearchWords(ByVal TableNames As String, ByVal strLocal As String, ByVal strWords As String)
        Try
            Dim oFile As System.IO.File ' Read text file
            Dim oWrite As System.IO.TextWriter  ' Write text file
            Dim lastFile As String
            Dim strHTML(20) As String
            Dim convertString, otherString, sCurrentDir As String
            Dim myHTML As New DicHTML
            Dim j As Integer

            strWord1 = strWords

            If InStr(strWords, "'") > 0 Then
                strWords = Replace(strWords, "'", "%")
            ElseIf InStr(strWords, "%") > 0 Then
                strWords = Replace(strWords, "%", "")
            ElseIf InStr(strWords, "?") > 0 Then
                strWords = Replace(strWords, "%", "")
            ElseIf InStr(strWords, "!") > 0 Then
                strWords = Replace(strWords, "%", "")
            End If

            If strWords.Length = 0 Then Exit Sub

            strHTML(1) = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
            strHTML(2) = "<html xmlns='http://www.w3.org/1999/xhtml'>"
            strHTML(3) = "<head>"
            strHTML(4) = "<title>Buddhism Dictionary</title><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"
            strHTML(5) = "<style type='text/css'> body {background:#FEF6D2;} ul{list-style:none;padding-left:30px;background: url(img/asterisk_orange.png) no-repeat 3px 4px;} li{list-style:none;padding-left:55px;} ol{list-style:none;padding-left:50px;background: url(img/bullet_green.png) no-repeat 25px 4px;}"
            strHTML(6) = "#dict {padding-bottom:5px;padding-left:30px;padding-right:10px;height:23px;width:100%;border-bottom:1px solid #EFE3AE;border-top:1px solid #EFE3AE;background:#ededed url(img/books.png) no-repeat 3px 4px;text-shadow:none;}"
            strHTML(7) = ".style1 {margin-top:5px;font-family:'Tahoma';font-weight:bold;color:#030000;} #pre {background: url(img/book_picture.png) no-repeat 3px 3px;}"
            strHTML(8) = "#words {padding-bottom:5px;padding-left:30px;background: url(img/words-a.png) no-repeat 3px 4px;}"
            strHTML(9) = ".style2 {margin-top:10px;margin-bottom:4px;font-family:'Tahoma';font-weight:bold;line-height: 2em;color:#94001f;}"
            strHTML(10) = "#speak {padding-bottom:0px;padding-left:30px;background: url(img/sound.png) no-repeat 3px 3px;}"
            strHTML(11) = ".style6 {margin-top:5px;margin-bottom:4px;font-family:'Tahoma';font-weight:normal;line-height: 2em;color:#3dc507;}"
            strHTML(12) = "#content {padding-left:30px;margin-bottom:4px;font-weight:normal;line-height:1.5em !important;}"
            strHTML(13) = ".style3 {margin-bottom:4px;font-family:'Tahoma';font-weight: normal;color:#000000;}"
            strHTML(14) = "#author {padding-left:30px;background: url(img/author.png) no-repeat 3px 3px;font-weight:normal;border-top:1px solid #EFE3AE;}.style4 {margin-top:4px;font-family:Tahoma;font-weight: normal;color:#999966;}"
            strHTML(15) = "</style></head><body>"
            strHTML(16) = "<div id='dict' class='dict'><h5 class='style1'>" ' Tao the dong
            strHTML(17) = "<div id='words' class='words'><h5 class='style2'>" ' Tao the dong
            strHTML(18) = "<div id='content' class='content'><h5 class='style3'>"
            strHTML(19) = "<div id='author' class='author'><h6 class='style4'>"
            strHTML(20) = "</body></html>"

            ' Load file last.dbn
            lastFile = CurDir() & "\data\keywords.html"
            sCurrentDir = CurDir() & "\data\Database1.accdb"
            Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
            ' Open the Biblio.mdb database. 
            Dim cn As New OleDbConnection()
            cn.ConnectionString = stConnectDB
            cn.Open()

            Dim cmd As New OleDbCommand("SELECT * FROM " & TableNames & " WHERE " & strLocal & " Like '" & strWords & "'", cn)
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            ' Read the names of all the publishers in the resultsets.
            oWrite = System.IO.File.CreateText(lastFile)
            For i = 1 To 15
                oWrite.WriteLine(strHTML(i))
            Next
            otherString = ""
            j = 0
            Do While dr.Read()
                oWrite.WriteLine(strHTML(16) & DictName & "</h5></div>")
                oWrite.WriteLine(strHTML(17) & dr.Item("Words") & "</h5></div>")
                If dr.IsDBNull(2) = False Then
                    oWrite.WriteLine("<div id='speak' class='content'><h5 class='style6'>" & dr.Item("Transcription") & "</h5></div>")
                Else
                    oWrite.WriteLine("<div id='speak' class='content'><h5 class='style6'>/</h5></div>")
                End If

                'If j = 0 Then
                oWrite.WriteLine("<div id='pre'>")
                'j = j + 1
                'End If
                convertString = Replace(dr.Item("Content"), vbCrLf, "</h5></div>" & strHTML(18))
                oWrite.WriteLine(strHTML(18) & convertString & "</h5></div>")
                strContent = convertString ' Speaker
                convertString = ""
                oWrite.WriteLine(strHTML(19) & DictAuthor & "</h6></div>")
            Loop
            oWrite.WriteLine("</div>")
            ' Close the DataReader and dispose of the Command. 
            oWrite.WriteLine(strHTML(20))
            oWrite.Flush()
            oWrite.Close() ' Close text file

            dr.Close()
            cmd.Dispose()
            cn.Close()

            myHTML.LoadHTML(Me.myWeb, lastFile)
            'cn.Close()
        Catch
            Exit Sub
        End Try
    End Sub

    Private Sub btClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClose.Click
        Me.Close()
    End Sub

    Private Sub panelTop_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelTop.MouseDown
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - Me.Left
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top
    End Sub

    Private Sub panelTop_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelTop.MouseMove
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
        End If
    End Sub

    Private Sub panelTop_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelTop.MouseUp
        drag = False
    End Sub

    Private Sub btMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMax.Click
        If FormMax = False Then
            GetMax()
            FormMax = True
        Else
            Me.Width = 870
            Me.Height = 500
            GetNormal()
            FormMax = False
            'btClose.Location.X(Me.Width - 30)
        End If
    End Sub

    Private Sub btMin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMin.Click
        Me.WindowState = FormWindowState.Minimized
        'If Me.WindowState.Minimized Then Me.WindowState = FormWindowState.Normal
    End Sub

    Private Sub listDictionary()
        Dim sCurrentDir As String

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 
        Dim cn As New OleDbConnection()
        cn.ConnectionString = stConnectDB
        cn.Open()

        Dim cmd As New OleDbCommand("SELECT * FROM _dict_profile", cn)
        ' Run the query; get the DataReader object.
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        ' Read the names of all the publishers in the resultsets.

        ' Mo table kiem tra da cai dat hay chua?
        Do While dr.Read()
            cboListDict.Items.Add(dr.Item("DictName"))
            If dr.Item("Status") = "Y" Then
                DictCode = dr.Item("DictCode")
                DictName = dr.Item("DictName")
                DictAuthor = dr.Item("DictAuthor")
                DictFont = dr.Item("DictFont")
                DictTable = dr.Item("DictTable")
                cboListDict.Text = DictName
            End If
        Loop

        cn.Close()
    End Sub

    Private Sub ReLoadDictionary(ByVal strDictionaryName As String)
        Dim sCurrentDir As String

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 
        Dim cn As New OleDbConnection()
        cn.ConnectionString = stConnectDB
        cn.Open()

        Dim da As New OleDbDataAdapter("SELECT * FROM _dict_profile", cn)
        Dim ds As New DataSet()
        da.FillSchema(ds, SchemaType.Source, "_dict_profile")
        da.Fill(ds, "_dict_profile")
        Dim cmdBuilder As New OleDbCommandBuilder(da)

        With ds.Tables("_dict_profile")
            For i = 0 To .Rows.Count - 1
                'strText = .Rows(i)("txt1")
                If .Rows(i)("Status") = "Y" Then
                    'MsgBox("have")
                    .Rows(i)("Status") = "N"
                End If
                If .Rows(i)("DictName") = strDictionaryName Then
                    DictCode = .Rows(i)("DictCode")
                    DictName = .Rows(i)("DictName")
                    DictAuthor = .Rows(i)("DictAuthor")
                    DictFont = .Rows(i)("DictFont")
                    DictTable = .Rows(i)("DictTable")
                    .Rows(i)("Status") = "Y"
                End If
            Next
        End With
        ' Send changes to the database, and disconnect.

        da.Update(ds, "_dict_profile")
        cn.Close()
    End Sub

    Private Sub LoadSpeaker()
        Try
            Dim Token As ISpeechObjectToken
            For Each Token In vox.GetVoices
                'MsgBox("" & Token.GetDescription().ToString)
                cboSpeaker.Items.Add(Token.GetDescription())
            Next
            If cboSpeaker.Items.Count > 0 Then
                cboSpeaker.SelectedIndex = 0
                checkSpeaker1.Checked = True
            Else
                cboSpeaker.Enabled = False
                checkSpeaker1.Enabled = False
                checkSpeaker2.Enabled = False
            End If

        Catch
            If cboSpeaker.Items.Count > 0 Then
                cboSpeaker.SelectedIndex = 0
                checkSpeaker1.Checked = True
            Else
                cboSpeaker.Enabled = False
                checkSpeaker1.Enabled = False
                checkSpeaker2.Enabled = False
            End If
        End Try
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myHTML As New DicHTML

        listDictionary()
        DrawTextbox()

        Timer1.Interval = 1000
        Timer1.Enabled = True
        AddHandler Timer1.Tick, AddressOf OnTimerEvent

        GetNormal()
        LoadLanguage()
        LoadSpeaker()
        myHTML.StartDict(Me.myWeb)
    End Sub

    Public Shared Sub OnTimerEvent(ByVal [source] As Object, ByVal e As EventArgs)

    End Sub 'OnTimerEvent

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'TxtToTable("vidu_01.tum", "Sheet1")
        'frmPleaseWait.Show()
        'Me.AddOwnedForm(frmPleaseWait)
        Timer1.Enabled = False
        'TummoFileName
        LoadCache2(DictTable)
    End Sub

    Private Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        ValueKeyboard()
        FormMax = False ' Form Normal        
        checkWord1.Select()
        LockCombo = 0
    End Sub
   
    Private Sub frmMain_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        cn.Close()
    End Sub

    Private Sub listWords_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles listWords.CellMouseClick
        If listWords.Rows.Count = 1 Then Exit Sub
        'MsgBox("" & listWords.Rows.Count)
        If checkWord1.Checked = True Then
            strWord1 = listWords.Rows.Item(e.RowIndex).Cells(0).Value.ToString()
            SearchWords(DictTable, "Words", strWord1)
            'txtSearch.Text = strWord1
            If cboKeyboard.Text = "English - US" Then
                txtUSKeyBoard.Text = strWord1
            ElseIf cboKeyboard.Text = "Pali - Latin" Then
                txtPaliLatin.Text = strWord1
            End If
        Else
            strWord1 = listWords.Rows.Item(e.RowIndex).Cells(1).Value.ToString()
            If strWord1 = "" Then Exit Sub
            SearchWords(DictTable, "Transcription", strWord1)
            'txtSearch.Text = strWord1
            If cboKeyboard.Text = "English - US" Then
                txtUSKeyBoard.Text = strWord1
            ElseIf cboKeyboard.Text = "Pali - Latin" Then
                txtPaliLatin.Text = strWord1
            End If
        End If
        
    End Sub

    Private Sub checkWord1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkWord1.Click
        If checkWord1.Checked = True Then isWordSelect = True
    End Sub

    Private Sub checkWord2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkWord2.Click
        If checkWord2.Checked = True Then isWordSelect = False
    End Sub
    
    
    Private Sub cboListDict_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboListDict.SelectedValueChanged
        If LockCombo = 1 Then
            ReLoadDictionary(cboListDict.Text)
            LoadCache2(DictTable)
        End If
    End Sub

    Private Sub cboListDict_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboListDict.Click
        LockCombo = 1
    End Sub

    Private Sub cboKeyboard_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboKeyboard.SelectedValueChanged
        DrawTextbox()
    End Sub

    Private Sub SplitContainer1_SplitterMoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer1.SplitterMoved
        ReTableSize()
    End Sub

    Private Sub btSpeaker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSpeaker.Click
        Dim RemoveHTMLTag As New DicHTML
        Dim strRemoved As String

        Try
            If strWord1 <> "" And cboSpeaker.Enabled = True Then
                If cboSpeaker.Items.Count > 0 Then vox.Voice = vox.GetVoices().Item(cboSpeaker.SelectedIndex)

                If checkSpeaker1.Checked = True Then
                    vox.Speak(strWord1)
                Else
                    strRemoved = RemoveHTMLTag.StripTags(strContent)
                    vox.Speak(strRemoved)
                End If
            End If
        Catch
        End Try

    End Sub
   
    Private Sub btStopSpeaker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStopSpeaker.Click
        vox.Pause()

    End Sub
    
    Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
        myWeb.ShowSaveAsDialog()
    End Sub

    Private Sub btPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPrint.Click
        myWeb.ShowPrintPreviewDialog()
    End Sub

    Private Sub btHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btHome.Click
        Dim myHTML As New DicHTML

        myHTML.StartDict(Me.myWeb)
    End Sub

    Private Sub myWeb_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles myWeb.DocumentCompleted
        Dim olink As HtmlElement
        Dim olinks As HtmlElementCollection = myWeb.Document.Links

        For Each olink In olinks
            olink.AttachEventHandler("onclick", AddressOf LinkClicked)
        Next
    End Sub

    Private Sub myWeb_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles myWeb.Navigating
        If bCancelLink = True Then
            e.Cancel = True
        End If
    End Sub

    Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim link As HtmlElement = myWeb.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")
        Dim strKetQua As String
        strKetQua = LCase(link.InnerText)

        SearchWords(DictTable, "Words", strKetQua)
        'bCancelLink = True

    End Sub
    
    Private Sub btAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAbout.Click
        frmAbout.Show()
        Me.AddOwnedForm(frmAbout)
    End Sub
End Class
